package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpSession;
import java.util.List;

public abstract class UserContext {

    public static final String EMPLOYEE_IN_SESSION = "EMPLOYEE_IN_SESSION";
    public static final String EXPRESSION_IN_SESSION = "EXPRESSION_IN_SESSION";

    // 往session存入登录用户
    public static void setCurrentUser(Employee emp) {
        getSession().setAttribute(EMPLOYEE_IN_SESSION, emp);
    }
    // 从session中删除登录用户
    public static void logoutCurrentUser() {
        getSession().removeAttribute(EMPLOYEE_IN_SESSION);
    }

    // 从session获取登录用户
    public static Employee getCurrentUser() {
        return (Employee) getSession().getAttribute(EMPLOYEE_IN_SESSION);
    }

    // 获取session对象
    public static HttpSession getSession() {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attrs.getRequest().getSession();
    }

    // 往session存入登录用户的权限信息
    public static void setExpressions(List<String> permissions) {
        getSession().setAttribute(EXPRESSION_IN_SESSION, permissions);
    }

    // 从session获取登录用户的权限信息
    public static List<String> getExpressions() {
        return (List<String>) getSession().getAttribute(EXPRESSION_IN_SESSION);
    }
}